/**
 * Notes: 预约模块业务逻辑
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2025-12-10 07:48:00
 */

const BaseBiz = require('@/utils/comm/biz/base_biz.js');
const pageHelper = require('@/utils/helper/page_helper.js');
const dataHelper = require('@/utils/helper/data_helper.js');
const projectSetting = require('@/utils/public/project_setting.js');
const api = require('@/api/api.js');
class MeetBiz extends BaseBiz {
    // 静态缓存变量
    static _cateListCache = null;
    static async subscribeMessageMeet(callback) {
        uni.requestSubscribeMessage({
            tmplIds: ['RYDxYPJynjoRcC9lLYGM8P1nuQr68f5sd7mQftVULgk', 'OTw2KKPEt_OVteo8yP10sLN8mWhMHwX9SRv8yVXgy28'],
            async complete() {
                if (callback) {
                    await callback();
                }
            }
        });
    }

    /** 初始化分类数据（预加载） */
    static async initCateList() {
        if (this._cateListCache !== null) {
            return this._cateListCache;
        }
        
        try {
            let res = await api.getCourseList();
            console.log('res', res);
            let arr = [];
            for (let k = 0; k < res.length; k++) {
                arr.push({
                    label: res[k].name,
                    type: 'cateId',
                    val: res[k].id,
                    value: res[k].id //for list
                });
            }
            this._cateListCache = arr;
            return arr;
        } catch (error) {
            console.error('获取课程列表失败:', error);
            this._cateListCache = [];
            return [];
        }
    }

    /** 取得分类（异步版本，保持向后兼容） */
    static getCateList() {
    
           // 如果缓存为空，返回空数组并在后台初始化
           if (this._cateListCache === null || this._cateListCache.length === 0) {
            this._cateListCache = [];
            // 后台异步初始化，不阻塞当前调用
            this.initCateList().catch(error => {
                console.error('后台初始化课程列表失败:', error);
            });
            return [];
        }
        return this._cateListCache;
    }

    /** 取得分类（同步版本，直接返回缓存） */
    static async getCateListSync() {
        // 如果缓存为空，返回空数组并在后台初始化
        return await this.initCateList();
    }

    /** 清除缓存（用于刷新数据） */
    static clearCateListCache() {
        this._cateListCache = null;
    }
        // let cateList = projectSetting.MEET_CATE;
        // console.log('cateList', cateList);
        // let arr = [];
        // for (let k = 0; k < cateList.length; k++) {
        //     arr.push({
        //         label: cateList[k].title,
        //         type: 'cateId',
        //         val: cateList[k].id,
        //         //for options
        //         value: cateList[k].id //for list
        //     });
        // }
        // return arr;
// }
    static setCateTitle() {
        return BaseBiz.setCateTitle(projectSetting.MEET_CATE);
    }
}
module.exports = MeetBiz;
